<?php
include('classes/config.inc.php');
include(DIR_CLASS . 'Session.php');
include(DIR_CLASS . 'Database.php');
include(DIR_CLASS . 'TemplateParser.php');
include(DIR_CLASS . 'Form.php');

$session = new UserSession();
$session->open();

$db = new MYSQLDatabase(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);
try {
		$db->connect();
}catch (Exception $e){
		echo $e->getMessage();
}
if($session->check())
	$menu = "<a href=\"profile.php\">Profil</a> / <a href=\"login.php?act=logout\">Logga ut</a>[". substr($session->getEmail(),0,12) ."] / <a href=\"\">Karta &ouml;ver hemsidan</a>";
else
	$menu = "<a href=\"register.php\">Registrera dig</a> / <a href=\"login.php\">Logga in</a> / <a href=\"sitemap.php\">Karta &ouml;ver hemsidan</a>";

if(!$session->check()){
	$menu = "<a href=\"register.php\">Registrera dig</a> / <a href=\"login.php\">Logga in</a> / <a href=\"sitemap.php\">Karta &ouml;ver hemsidan</a>";
	$template_array = array("MENU" => $menu);
	
	$template = new TemplateParser();
	$template->loadfile("templates/login_first.tpl");
	$template->assignVars($template_array);
	$template->output();
	$db->disconnect();
}
elseif(isset($_POST['action']) && $_POST['action'] == 'addJob'){
		$form = new Form();
		$formElements = array('title','descrip','fee','date');
		$reqElements = array('title','descrip','fee','date');
		$form->open($formElements,$reqElements);
		
		$captcha = new Captcha();

		if($form->isValidForm() && $captcha->check($form->getElement('captcha'))){
			$verify = "SELECT * FROM ". TABLE_JOBS ." WHERE(title=". $form->get("title") .")";
			$sql = "INSERT INTO ". TABLE_JOBS ." (name, lastName, email, password) VALUES (".$form->get("name").", ".$form->get("lastName").", ".$form->get("email")."))";
			$db = new MYSQLDatabase(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);
			try {
				$db->connect();
				$link = $db->query($verify);
				if(mysql_num_rows($link)<=0){
					$link = $db->query($sql);
					if(mysql_num_rows($link)<=0){
						$mailer = new Mail("Registratie","email :");
						$mailer->send($form->get("email"));
						if($mailer->isSended()){
							echo "verzondern...";
						}
					}
				}
			}catch (Exception $e){
				
			}
		}
		else{
			//incorrecte data
			$captcha = new Captcha();
			$captcha->open();
			$captcha->save();
		}
}else{
	
	$sql = "SELECT * FROM ". TABLE_CAT." ORDER BY `Cat`.`title` ASC";	
	$cat= "";
	
	$link = $db->query($sql);
	if(mysql_num_rows($link)>0){
		while($res = mysql_fetch_array($link)){
			$cat .= "<input type=\"radio\" name=\"cats\" value=\"".$res["id"]."\">".$res["title"]."<br />";
		}
	}
	else{
		$error="Could not load Categories!";
	}
	$captcha = new Captcha();
	$captcha->open();
	$captcha->save();
	
	$template_array = array(
			"MENU" => $menu,
			"ERROR" => isset($error)?$error:"",
			"CAPTCHA" => $captcha->getQuestion(),
			"TITEL"=> "",
			"DESC" => "",
			"FEE" => "",
			"CATS" => $cat,
			"DEATHLINE" => date("d-m-Y"),
		);
		
	$template = new TemplateParser();
	$template->loadfile("templates/employer.tpl");
	$template->assignVars($template_array);
	$template->output();
	$db->disconnect();
}
			